<template>
  <div>
    <el-form :inline="true" :model="dataForm">
      <el-form-item>
        <el-input v-model="dataForm.attrName" placeholder="组名" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getAttr()">查询</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="dialogFormVisible = true">新增</el-button>
      </el-form-item>
    </el-form>

    <el-table
      :data="attrList"
      stripe
      border
      style="width: 100%"
      highlight-current-row>

      <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>
      <el-table-column prop="attrId" header-align="center" align="center" label="属性id"></el-table-column>
      <el-table-column prop="attrName" header-align="center" align="center" label="名称"></el-table-column>
      <el-table-column prop="enable" label="是否检索" width="180" align="center">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.searchType" active-color="#13ce66" inactive-color="#ff4949" :active-value="1"
                     :inactive-value="0"></el-switch>
        </template>
      </el-table-column>
      <el-table-column prop="enable" label="是否启用" width="180" align="center">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.enable" active-color="#13ce66" inactive-color="#ff4949" :active-value="1"
                     :inactive-value="0"></el-switch>
        </template>
      </el-table-column>

      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作"
      >
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="updateHandle(scope.row.attrId)"
          >修改</el-button>
          <el-button type="text" size="small" @click="deleteAttr(scope.row.attrId)">删除</el-button>
        </template>
      </el-table-column>

    </el-table>

    <!-- 分页 -->
    <el-pagination
      background
      :current-page="page"
      :page-size="limit"
      :total="total"
      style="padding: 30px 0; text-align: center;"
      layout="total, prev, pager, next, jumper"
      @current-change=""
    />

    <!-- 新增 -->
    <el-dialog title="规格参数" :visible.sync="dialogFormVisible">
      <el-form :model="attr">
        <el-form-item label="名称" :label-width="formLabelWidth">
          <el-input v-model="attr.attrName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="是否检索" :label-width="formLabelWidth">
          <el-switch v-model="attr.searchType" active-color="#13ce66" inactive-color="#ff4949" :active-value="1"
                     :inactive-value="0"></el-switch>
        </el-form-item>
        <el-form-item label="图标" :label-width="formLabelWidth">
          <el-input v-model="attr.icon" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="启用状态" :label-width="formLabelWidth">
          <el-switch v-model="attr.enable" active-color="#13ce66" inactive-color="#ff4949" :active-value="1"
                     :inactive-value="0"></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogFormVisible = false, addAttr()">确 定</el-button>
      </div>
    </el-dialog>

    <!-- 修改 -->
    <el-dialog title="规格参数" :visible.sync="dialogUpdate">
      <el-form :model="attrUpdate">
        <el-form-item label="名称" :label-width="formLabelWidth">
          <el-input v-model="attrUpdate.attrName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="是否检索" :label-width="formLabelWidth">
          <el-switch v-model="attrUpdate.searchType" active-color="#13ce66" inactive-color="#ff4949" :active-value="1"
                     :inactive-value="0"></el-switch>
        </el-form-item>
        <el-form-item label="图标" :label-width="formLabelWidth">
          <el-input v-model="attrUpdate.icon" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="启用状态" :label-width="formLabelWidth">
          <el-switch v-model="attrUpdate.enable" active-color="#13ce66" inactive-color="#ff4949" :active-value="1"
                     :inactive-value="0"></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogUpdate = false">取 消</el-button>
        <el-button type="primary" @click="dialogUpdate = false, updateAttr()">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import attr from "@/api/game/attr";

export default {
  name: "attr",
  data() {
    return {
      dataForm: {},
      attrList: [], //游戏列表
      attr: {},
      formLabelWidth: '150',
      dialogFormVisible: false,
      dialogUpdate: false,
      attrUpdate: {},
      page: 1,  //当前页码
      limit: 20,  // 每页显示的商品数量
      total: 0,  // 商品总量
    };
  },
  activated() {
    this.getAttr()
  },
  methods: {
    getAttr(page = 1) {
      this.page = page
      const params = {
        page: this.page,
        key: this.dataForm.attrName
      }
      attr.getAttrInfo(params)
      .then(result => {
        this.total = result.data.totalCount
        this.attrList = result.data.list
      })
    },

    //添加游戏配置参数
    addAttr() {
      attr.addAttrInfo(this.attr)
      .then(result => {
        this.getAttr()
      })
    },

    updateHandle(val) {
      attr.getAttrInfoById(val)
      .then(result => {
        this.attrUpdate = result.data
        this.dialogUpdate = true
      })
    },

    updateAttr() {
      attr.updateAttrInfo(this.attrUpdate)
      .then(result => {
        this.getAttr()
      })
    },

    deleteAttr(val) {
      this.$confirm(
        `确定对[id = ${val}]操作?`,
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      ).then(reset => {
        attr.deleteAttrInfoById(val)
        .then(result => {
          this.getAttr()
        })
      })
    }
  }
}
</script>

<style scoped>

</style>
